<!--
@name:prescriptionList
@author:Zhenglei
@date:16:46
@description:prescriptionList
@update:2022/5/1116:46
-->
<template>
  <div class="pd-20">
    <el-select v-model="queryData.status"
               clearable
               @change="handleOption('select')"
               placeholder="请选择审核状态">
      <el-option v-for="item in auditStautsList"
                 :key="item.id"
                 :label="item.name"
                 :value="item.id">
      </el-option>
    </el-select>
    <element-table class="mt-20"
                   :columns="columns"
                   :data="tableData"
                   v-loading="loading"
                   pager
                   :pageTotal="pageTotal"
                   :pageIndex="pageIndex"
                   :pageSize="pageSize"
                   @handleChangePage="handleChangePage">
      <template v-slot:status="{row}">
        <el-tag type="danger"
                v-if="row.status===-1">审核被拒绝
        </el-tag>
        <el-tag type="warning"
                v-if="row.status===0">待审核
        </el-tag>
        <el-tag type="success"
                v-if="row.status===1">审核通过
        </el-tag>
      </template>
      <template v-slot:option="{row}">
        <el-button type="primary"
                   v-if="row.status===0"
                   @click="handleOption('transition', row)">审核
        </el-button>
        <el-button type="warning"
                   v-else
                   @click="handleOption('detail', row)">详情
        </el-button>
      </template>
    </element-table>

    <audit-dialog :dataObj="dataObj"
                   v-if="showDialog"
                   :visible.sync="showDialog"/>
  </div>
</template>

<script>
import ElementTable from "../../../../base/ElementTable";
import auditDialog from "./auditDialog";

export default {
  name: "prescriptionList",
  data() {
    return {
      queryData: {
        status: ''
      },
      loading: false,
      pageTotal: 0,
      pageSize: 20,
      pageIndex: 1,
      tableData: [],
      dataObj: {},
      showDialog: false,
      auditStautsList: [{name: '审核被拒', id: -1}, {name: '待审核', id: 0}, {name: '审核通过', id: 1}],
      columns: [
        {
          label: '处方类型',
          prop: 'prescriptionType',
          align: "center",
          formatter: (row, cellValue) => {
            if (row.prescriptionType === 1) {
              return '中药处方'
            } else if (row.prescriptionType === 2) {
              return '西药/中成药处方'
            } else if (row.prescriptionType === 4) {
              return '远程监测处方'
            } else {
              return ''
            }
          },
        }, {
          label: '患者姓名',
          prop: 'memberName',
          align: "center",

        }, {
          label: '性别',
          prop: 'memberSex',
          align: "center",
          formatter: (row, cellValue) => {
            if (row.memberSex === 1) {
              return '男'
            } else if (row.memberSex === 2) {
              return '女'
            } else {
              return ''
            }
          },

        }, {
          label: '年龄',
          prop: 'memberAge',
          align: "center",
          formatter: (row, cellValue) => {
            return this.getAge(row.memberAge)
          },
        }, {
          label: '开方医生',
          prop: 'doctorName',
          align: "center",

        }, {
          label: '审核状态',
          prop: 'status',
          align: "center",
          isSlot: true,

        }, {
          label: '操作',
          prop: 'option',
          align: 'center',
          isSlot: true,
          width: 200
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleOption(op, row) {
      switch (op) {
        case 'select':
          this.pageIndex=1
          this.getList()
          break
        case 'detail':
          this.showDialog = true
          this.dataObj = row
          this.dataObj.isEdit=false
          break
        case 'transition':
          this.showDialog = true
          this.dataObj = row
          this.dataObj.isEdit=true
          break
      }
    },
    getList() {
      this.loading = true
      this.post(this.api.slowDisease.getToViewList, {
        pageSize: this.pageSize,
        pageIndex: this.pageIndex - 1,
        ...this.queryData
      }).then((res) => {
        this.loading = false
        if (res.data.code === '200') {
          this.tableData = res.data.object
          this.pageTotal = res.data.totalNum;
        }
      })
    },
    // 时间转换
    formatterDate(time) {
      if (time)
        return this.commonFunction.timestampFormat(new Date(time), 'yyyy-MM-dd hh:mm')
      else
        return ''
    },
    getAge(birth) {
      if (birth){
        let birthdays = new Date(birth);
        let d = new Date();
        let age =
          d.getFullYear() -
          birthdays.getFullYear() -
          (d.getMonth() < birthdays.getMonth() ||
          (d.getMonth() == birthdays.getMonth() &&
            d.getDate() < birthdays.getDate())
            ? 1
            : 0);
        return age
      }else {
        return birth
      }

    },
    // 分页
    handleChangePage(page) {
      this.pageIndex = page;
      this.getList();
    },
  },
  components: {
    'element-table': ElementTable,
    'audit-dialog':auditDialog
  },
}
</script>

<style scoped>

</style>
